package com.xnyzc.lhy.mis.controller.cmsapi.office;

import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.mis.entity.param.cms.OfficeParam;
import com.xnyzc.lhy.mis.entity.param.cms.UpdateOfficeParam;
import com.xnyzc.lhy.mis.service.cms.office.OfficeService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 机构相关
 *
 * @Author: wangshuo
 * @Date: 2019-08-07 14:11
 */
@RestController
@RequestMapping("/cmsapi/office")
public class OfficeController {

    @Autowired
    private OfficeService officeService;

    /**
     * 根据条件获取供应商列表
     */
    @ApiOperation(value = "获取机构数据", notes = "获取机构数据")
    @GetMapping(value = "/getOfficesByConditions")
    public Rv getOfficesInfoByConditions(@RequestParam(value = "cityName") String cityName,
                                         @RequestParam(value = "officeName")String officeName,
                                         @RequestParam(value = "currentPage")Integer currentPage,
                                         @RequestParam(value = "pageSize")Integer pageSize) {
        return officeService.getOfficesByConditions(cityName,officeName,currentPage,pageSize);
    }
    /**
     * 获取机构JSON数据。
     *
     * @return
     */
    @ApiOperation(value = "获取机构数据", notes = "获取机构数据")
    @PostMapping(value = "/getOfficeFindList")
    public Rv getOfficeFindList(
            @ApiParam(name = "officeParam", value = "机构请求类") @RequestBody OfficeParam officeParam
    ) {
        return Rv.wrap(officeService.getOfficeFindList(officeParam));
    }

    /**
     * 获取点击机构列表
     *
     * @return
     */
    @ApiOperation(value = "获取机构列表", notes = "获取机构列表")
    @PostMapping(value = "/getClickOfficeList")
    public Rv getClickOfficeList(
            @ApiParam(name = "officeParam", value = "机构请求类") @RequestBody OfficeParam officeParam
    ) {
        return Rv.wrap(officeService.getClickOfficeList(officeParam));
    }

    /**
     * 获取机构详情
     *
     * @return
     */
    @ApiOperation(value = "获取机构详情", notes = "获取机构详情")
    @PostMapping(value = "/getOfficeDetail")
    public Rv getOfficeDetail(
            @ApiParam(name = "officeParam", value = "机构请求类") @RequestBody OfficeParam officeParam
    ) {
        return Rv.wrap(officeService.getOfficeDetail(officeParam));
    }

    /**
     * 机构新增、修改
     *
     * @return
     */
    @ApiOperation(value = "机构新增、修改", notes = "机构新增、修改")
    @PostMapping(value = "/getUpdateOffice")
    public Rv getUpdateOffice(
            @ApiParam(name = "UpdateOfficeParam", value = "修改机构请求类") @RequestBody UpdateOfficeParam param
    ) {
        return Rv.wrap(officeService.getUpdateOffice(param));
    }

    /**
     * 机构删除
     *
     * @return
     */
    @ApiOperation(value = "机构删除", notes = "机构删除")
    @PostMapping(value = "/getDeleteOffice")
    public Rv getDeleteOffice(
            @ApiParam(name = "officeParam", value = "机构请求类") @RequestBody OfficeParam officeParam
    ) {
        return Rv.wrap(officeService.getDeleteOffice(officeParam));
    }

    /**
     * 机构负责人列表
     *
     * @return
     */
    @ApiOperation(value = "机构负责人列表", notes = "机构负责人列表")
    @PostMapping(value = "/getOfficeUserList")
    public Rv getOfficeUserList(
            @ApiParam(name = "officeParam", value = "修改机构请求类") @RequestBody OfficeParam officeParam
    ) {
        return Rv.wrap(officeService.getOfficeUserList(officeParam));
    }

    /**
     * 获取只限于公司数据
     *
     * @return
     */
    @ApiOperation(value = "获取只限于公司数据", notes = "获取只限于公司数据")
    @PostMapping(value = "/getOfficeCompanyList")
    public Rv getOfficeCompanyList(
            @ApiParam(name = "officeParam", value = "机构请求类") @RequestBody OfficeParam officeParam
    ) {
        return Rv.wrap(officeService.getOfficeCompanyList(officeParam));
    }

    @ApiOperation(value = "根据城市编码查询运力供应商", notes = "根据城市编码查询运力供应商")
    @PostMapping(value = "/getOfficeList")
    public Rv getOfficeList(
            @ApiParam(name = "officeParam", value = "机构请求类") @RequestBody UpdateOfficeParam officeParam
    ) {
        return officeService.getOfficeList(officeParam);
    }

    @ApiOperation(value = "批量更新供应商分单", notes = "批量更新供应商分单")
    @PostMapping(value = "/updateOffice")
    public Rv getOfficeList(
            @ApiParam(name = "officeParam", value = "机构请求类") @RequestBody List<OfficeParam> officeParamList
    ) {
        long sysUserId = TokenUtil.getTokenUserId();
        String tag = TokenUtil.getTag();
        if(CheckUtil.objIsEmpty(tag,sysUserId)){
            throw PangException.create(EErrorCode.missingArg);
        }
        Double expressSum = 0D;
        Double compactSum = 0D;
        Double premiumSum = 0D;
        Double luxurySum = 0D;
        for (OfficeParam param :
                officeParamList) {//计算
            if (null==param.getOfficeId()){
                return Rv.wrap(EErrorCode.noData);
            }
            expressSum += "".equals(param.getDistributionExpress()) ? 0 : Double.parseDouble(param.getDistributionExpress());
            compactSum += "".equals(param.getDistributionCompact()) ? 0 : Double.parseDouble(param.getDistributionCompact());
            premiumSum += "".equals(param.getDistributionPremium()) ? 0 : Double.parseDouble(param.getDistributionPremium());
            luxurySum += "".equals(param.getDistributionLuxury()) ? 0 : Double.parseDouble(param.getDistributionLuxury());
        }
        if (100 != expressSum || 100!= compactSum || 100!=premiumSum || 100!=luxurySum) {
            return Rv.wrap(EErrorCode.noData);
        }
        return officeService.updateOffice(officeParamList,sysUserId);
    }

    public static void main(String[] args) {
        String a = "";
        System.out.println(Double.parseDouble(a));
    }


}
